package com.stacktry.funtv;

import android.util.Log;

import androidx.annotation.NonNull;

import com.fun.xm.FSPlayer;
import com.funshion.playsdk.callback.FunshionPlayInitCallback;
import com.funshion.playsdk.register.IAuthCodeGetter;
import com.stacktry.funtv.utils.AuthCodeGetter;
import com.stacktry.funtv.utils.Player;

import java.util.HashMap;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FuntvPlugin */
public class FuntvPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Player player;
  boolean isInit = false;
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "funtv");
    channel.setMethodCallHandler(this);
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());

    String appId = "hc4mrvhjiuonyfc5";
    IAuthCodeGetter authCodeGetter = new AuthCodeGetter("https://tkh5.huohua360.com/api/funtv/auth-code");
    FSPlayer.init(flutterPluginBinding.getApplicationContext(), appId,authCodeGetter, new FunshionPlayInitCallback() {
      @Override
      public void onSuccess() {
        isInit = true;
        Log.i("SDK初始化","[onSuccess]");
      }

      @Override
      public void onFail(int code, String msg) {
        Log.i("SDK初始化","[onFail]code = "+code + ",msg = "+msg);
      }
    });

    player = new Player(flutterPluginBinding);
    player.createPlayer();
  }

  private static class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
      Log.i("FSAD_ERROR", e.getMessage());
      e.printStackTrace();
    }
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if(call.method.equals("init")){
      //funtvInit("hc4mrvhjiuonyfc5","http://8.136.226.26:9988/test.php",result);
    }else if(call.method.equals("getVideoUrl")){
      HashMap<String, String> params = new HashMap<String, String>();
      params.put("ceCode",call.<String>argument("ceCode"));
      params.put("cmCode",call.<String>argument("cmCode"));
      params.put("uid",call.<String>argument("uid"));

      params.put("flag",call.<String>argument("flag"));

      player.playLongMedia(params,result); // E05CN97HR2
    }else if(call.method.equals("isInit")){
      result.success(isInit);
    }

    else if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
